package schoolsource.model;

import schoolsource.sql.DBSQLAccessor;

/**
 *
 * @author Steven Steele
 * Copyright 2003
 *
 * Item containing a boolean value, database tools for saving and loading it, its required-ness,
 * and validation tools.
 */
public class BooleanItem extends Item {

	// The boolean stored in this Item
	private Boolean value;

	/**
	 * Returns a new Item with the database field name, value, and required-ness set
	 * to the parameter values.
	 * @param newField The name of the database field where the boolean value is stored.
	 * @param newVal The value of the boolean. This may either be 'true' or 'false'.
	 * @param newReq Whether the boolean value may be unset. This parameter is meaningless, since
	 * the value is always either true or false.
	 */
	public BooleanItem(String newField, String newVal, boolean newReq) {
		setFieldName(newField);
		setValue(newVal);
		setRequired(newReq);
	}

	/**
	 * Returns a new BooleanItem with the database field name, value, and required-ness set
	 * to the parameter values.
	 * @param newField The name of the database field where the boolean value is stored.
	 * @param newVal The value of the boolean.
	 * @param newReq Whether the boolean value may be unset. This parameter is meaningless, since
	 * the value is always either true or false.
	 */
	public BooleanItem(String newField, Boolean newVal, boolean newReq) {
		setFieldName(newField);
		setTrueValue(newVal);
		setRequired(newReq);
	}

	/**
	 * Sets the value using a string.
	 * @param newValue The value to set the boolean. The value of the Item is set
	 * to true if 'true' is passed in (case-insensitive) or false otherwise.
	 */
	public void setValue(String newValue) {
		if (newValue != null
			&& (newValue.equalsIgnoreCase("TRUE")
				|| newValue.equalsIgnoreCase("yes"))) {
			setTrueValue(new Boolean(true));
		} else {
			setTrueValue(new Boolean(false));
		}
		validate();
	}

	/**
	 * Sets the value using a Boolean object.
	 * @param newTrueValue An object that can be cast to a Boolean.
	 */
	public void setTrueValue(Object newTrueValue) {
		if (newTrueValue == null) {
			value = new Boolean(false);
		} else {
			value = (Boolean) newTrueValue;
		}

		validate();
	}

	/**
	 * Returns the string representation of the Item.
	 * @return The string representation of the Item.
	 */
	public String toString() {
		String returnVal = "";
		if (value != null) {
			returnVal = value.toString();
		}
		return returnVal;
	}

	/**
	 * Returns the Boolean object associated with the Item.
	 * @return The Boolean object associated with the Item.
	 */
	public Object getObject() {
		return value;
	}

	/**
	 * Returns a string formatted in the appropriate manner for a boolean value,
	 * according to the DBSQLAccessor passed in.
	 * @param dba A DBSQLAccessor that defines how values are represented in the
	 * target database.
	 */
	public String getDBFormattedString(DBSQLAccessor dba) {
		return dba.getDBFormattedBoolean(toString());
	}

	/**
	 * Sets the value of the Item according to the interpretation of a database representation
	 * of the Item using the passed-in DBSQLAccessor to interpret it.
	 * @param dbString The database representation of the Item.
	 * @param dba The DBSQLAccessor that defines how values are translated from the database
	 * representation to the Item value.
	 */
	public void createFromDBString(String dbString, DBSQLAccessor dba) {
		setValue(dba.removeDBTagsFromBoolean(dbString));
	}

	/**
	 * Indicates whether the Item is valid. This value is current only after the Item
	 * has validate() called on it.
	 * @return A boolean indicating whether the Item is valid.
	 */
	public boolean isValid() {
		return valid;
	}

	/**
	 * Processes the Item and determines whether it is valid strictly according to rules
	 * governing the Item implementation and whether the Item is required.
	 */
	public void validate() {
		if (value == null && isRequired()) {
			setErrorString("This field is required and must be either \"true\" or \"false\".");
			setValid(false);
		} else {
			setValid(true);
		}
	}

    public String getCheckedString() {
        if (value != null && value.booleanValue()) {
            return "CHECKED";
        }
        else {
            return "";
        }
    }

    public boolean booleanValue() {
        if (value != null) {
            return value.booleanValue();
        }
        else {
            return false;
        }
    }
}